package com.zyd.shiro.business.controller.pak;

import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Migrate;
import com.zyd.shiro.business.entity.Transfer;
import com.zyd.shiro.business.service.PakAssetsService;
import com.zyd.shiro.business.service.PakTransferService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.framework.object.PageResult;
import com.zyd.shiro.framework.object.ResponseVO;
import com.zyd.shiro.util.ResultUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.zyd.shiro.persistence.beans.PakTransfer;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.io.File;
import com.zyd.shiro.utils.FileUtils;
import com.zyd.shiro.utils.ExcelUtil;
import java.util.Date;


@RestController
@RequestMapping("/transfer")
@Api(description = "资产调拨")
public class PakTransferController {
    @Autowired
    private PakTransferService transferService;

    @Autowired
    private PakAssetsService assetsService;

    @PostMapping
@ApiOperation(value="添加", produces="application/json")
    public ResponseVO insert(Transfer transfer) {
        transferService.insert(transfer);
        return ResultUtil.success("添加成功！",transfer);
    }

    @PutMapping
    @ApiOperation(value="修改", produces="application/json")
    public ResponseVO update(Transfer transfer) {
        transferService.updateSelective(transfer);
        return ResultUtil.success("修改成功！",transfer);
    }

    @DeleteMapping(value = "/{id}")
    @ApiOperation(value="主键删除", produces="application/json")
    public ResponseVO delete(@PathVariable(name="id") Long id){
        transferService.removeByPrimaryKey(id);
        return ResultUtil.success("删除成功！",null);
    }

    @GetMapping
    @ApiOperation(value="分页查询", produces="application/json")
    public PageResult page(BaseConditionVO vo) {
        PageInfo<Transfer> pageInfo = transferService.findPageBreakByCondition(vo);
        return ResultUtil.tablePage(pageInfo);
    }

    @GetMapping(value = "/all")
    @ApiOperation(value="查询所有", produces="application/json")
    public ResponseVO list() {
        List<Transfer> transfers = transferService.listAll();
        return ResultUtil.success("查询成功！",transfers);
    }

/*    @PostMapping(value = "/import")
    @ApiOperation(value="批量导入", produces="application/json")
    public ResponseVO folderImport(MultipartFile file) throws Exception {
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.indexOf("."), fileName.length());
        if (suffix.equals(".xlsx") || suffix.equals(".xls")) {
            File newFile = new File(FileUtils.createFilePath(fileName));
            file.transferTo(newFile);
            ExcelUtil<PakTransfer> excelUtil = new ExcelUtil<>();
            List<PakTransfer> acfForms = excelUtil.importExcel("Sheet1", newFile, PakTransfer.class);
            for (PakTransfer a : acfForms) {
                a.setCreateTime(new Date());
                a.setUpdateTime(new Date());
                transferService.increase(a);
            }
            return ResultUtil.success("修改成功！");
        }
        return ResultUtil.error("请输入excel文件!");
    }*/

    @PutMapping(value = "/approve/{id}")
    @ApiOperation(value="审批", produces="application/json")
    public ResponseVO approve(@PathVariable(name="id") Long id,int status) {
        Transfer transfer = transferService.getByPrimaryKey(id);
        if(status==1){
            assetsService.transfer(transfer.getPakTransfer());
        }
        transfer.getPakTransfer().setAppstatus(status);
        transferService.updateSelective(transfer);
        return ResultUtil.success("审批成功！",transfer);
    }
}